<template>
  <div class="forget-password">
    <MyHeader>
      <div slot="title">账号安全</div>
    </MyHeader>
    <div class="g-form">
      <el-form :model="forgetForm" :rules="forgetRules" ref="forgetForm">
        <el-form-item label="旧密码" prop="old_password">
          <el-input v-model="forgetForm.old_password" show-password placeholder="旧密码"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="new_password">
          <el-input v-model="forgetForm.new_password" show-password placeholder="新密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input show-password v-model="forgetForm.confirmPassword" placeholder="请再次输入密码"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div class="line"></div>
    <div class="btn-next" @click="next('forgetForm')">下一步</div>
  </div>
</template>

<script>
import MyHeader from './common/myHeader'
export default {
  components: {
    MyHeader
  },
  data() {
    const validatePassword = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请确认密码'))
      }
      console.log(value, this.forgetForm.password)
      if (value != this.forgetForm.new_password) {
        callback(new Error('两次输入密码不一致，请重新输入'))
        this.forgetForm.confirmPassword = ''
      } else {
        callback()
      }
    }
    return {
      forgetForm: {
        reciever: '',
        new_password: '',
        old_password: '',
        confirmPassword: ''
      },
      codeMessage: '获取验证码',
      wait: false,
      timer: null,
      forgetRules: {
        old_password: [
          { required: true, message: '密码不能为空', trigger: 'change' }
        ],
        new_password: [
          { required: true, message: '密码不能为空', trigger: 'change' },
          { min: 8, message: '密码长度至少为8位', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'change' },
          { validator: validatePassword, trigger: 'blur' }
        ]
      },
      option: {
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      }
    }
  },
  methods: {
    next(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$loading(this.option)
          const params = {
            old_password: this.$md5(this.forgetForm.old_password),
            new_password: this.$md5(this.forgetForm.new_password),
          }
          this.$api.editPassword({ ...params }, { op: 'update_password', data_id: this.$store.state.user?.info?.user_info?.data_id }).then(res => {
            if (res.status == '$SUCCESS') {
              this.$loading().close()
              this.$refs.forgetForm.resetFields()
              this.$message.success('密码修改成功')
            } else {
              this.$message.error(res.message)
            }
          }).finally(() => {
            this.$loading().close()
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.forget-password {
  max-width: 1368px;
  // margin: 0 auto;
  // padding: 60px 0 0;
  background-color: #fff;

  .g-title {
    margin-bottom: 40px;
    .left {
      font-size: 30px;
      font-family: Source Han Serif SC;
      font-weight: 800;
      color: #000000;
      line-height: 30px;
    }

    .right {
      float: right;
      font-size: 16px;
      color: #333;

      .go-login {
        color: #AA9884;
        cursor: pointer;
      }
    }
  }

  .forget-password-body {
    background: #fff;
    padding: 60px 208px 60px 208px;
  }

  .g-form {
    width: 582px;
    position: relative;
    margin: 0 auto;
  }

  .line {
    width: 100%;
    height: 1px;
    background: #E8E8E8;
    transform: scaleY(0.8);
    margin: 40px 0;
  }

  .btn {
    width: 220px;
    height: 48px;
    background: #E0CBB4;
    color: #333;
    line-height: 48px;
    text-align: center;
    margin: 0 auto;
    cursor: pointer;
    font-size: 16px;

    &.el-button {
      line-height: 22px;
      display: block;
      margin: 0 auto;
      border-radius: 0;
    }
  }

  .disabledBtn {
    pointer-events: none;
  }

  .btnBox-wrap {
    position: absolute;
    right: 0;
    bottom: 22px;
  }

  .btn-next {
    width: 220px;
    height: 48px;
    margin: 0 auto;
    background: #E0CBB4;
    line-height: 48px;
    text-align: center;
    color: #333;
    font-size: 16px;
    cursor: pointer;
  }

  ::v-deep(.el-input__inner) {
    background-color: #f3f4f4;
    border-radius: 0;
    height: 50px;
  }

  ::v-deep(input::-webkit-input-placeholder) {
    color: #999;
  }

  ::v-deep(.el-form-item__content) {
    line-height: normal;
  }
}
</style>